package com.wlanauth.dao.manage.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.dao.DataAccessException;

import com.wlanauth.dao.common.impl.CommonDaoImpl;
import com.wlanauth.dao.manage.RoleDao;
import com.wlanauth.domain.manage.Role;

public class RoleDaoImpl extends CommonDaoImpl<Role> implements RoleDao {

	public Role getByCode(String code) throws DataAccessException {
		String hql = "from Role r where r.code='%s'";
		@SuppressWarnings("unchecked")
		List<Role> list = this.getHibernateTemplate().find(String.format(hql, code));
		if(list == null || list.size()<1) return null;
		if(list.size()>1){
			throw new RuntimeException("业务数据错误!期待返回最多1个结果,实际返回结果数:"+list.size());
		}
		
		return list.get(0);
	}

	@Override
	public Role get(Serializable key) throws DataAccessException {
		Role r =  super.get(key);
		if(r!=null){
			this.getHibernateTemplate().initialize(r.getSystemResources());
		}
		return r;
	}


}
